<?php
 
class DeliveredorderController extends BaseController {

//delivered oerder
		public function getDeliveredorder()
	{	
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		Session::forget('dateby');
		Session::forget('datestart');
		Session::forget('dateend');
		$deliveredorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Delivered')
						->orderBy('order_date','DESC')
						->groupBy('order_id')
						->paginate(5);
		return View::make('admin.order.delivered.deliveredorder')->with('deliveredorder',$deliveredorder);
	}

	public function postDeliveredorder()
	{	
		if(Input::get('filter')=='date'){
			Session::forget('list');
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','date');
				if(Input::get('datestart')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
				if(Input::get('dateend')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
			Session::put('datestart',Input::get('datestart'));
			Session::put('dateend',Input::get('dateend'));
			Session::put('dateby',Input::get('dateby'));

					if(Input::get('dateby')=='all'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Input::get('dateby')=='dateorder'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Pending')
								->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Input::get('dateby')=='dateapprove'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Approve')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Input::get('dateby')=='dateprocess'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','On Process')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Input::get('dateby')=='datefinished'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Finished')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Input::get('dateby')=='datedelivered'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Delivered')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}
					return View::make('admin.order.delivered.deliveredorder')->with('deliveredorder',$deliveredorder);
		}else if(Input::get('filter')=='type'){
			Session::forget('filterby');
			Session::forget('keyword');
			Session::forget('dateby');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
			$deliveredorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Delivered')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else{
			$deliveredorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Delivered')
						->where('order_name','=',Input::get('typeby'))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}
			return View::make('admin.order.delivered.deliveredorder')->with('deliveredorder',$deliveredorder);
		}else if(Input::get('filter')=='keyword'){
			Session::put('filter','keyword');
			Session::forget('list');
			Session::forget('dateby');
			Session::put('filterby',Input::get('filterby'));
			Session::put('keyword',Input::get('keyword'));
			if(Input::get('filterby')=='code'){
				$deliveredorder = DB::table('sys_order')
							->join('sys_customer','customer_id','=','order_customer_id')
							->join('sys_order_status','order_status_order_id','=','order_id')
							->join('sys_invoice','invoice_order_id','=','order_id')
							->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
							->where('order_status','=','Delivered')
							->where('order_code','LIKE','%'.Input::get('keyword').'%')
							->groupBy('order_id')
							->orderBy('order_date','DESC')
							->paginate(5);
				return View::make('admin.order.delivered.deliveredorder')->with('deliveredorder',$deliveredorder);
			}else if(Input::get('filterby')=='name'){
				$deliveredorder = DB::table('sys_order')
							->join('sys_customer','customer_id','=','order_customer_id')
							->join('sys_order_status','order_status_order_id','=','order_id')
							->join('sys_invoice','invoice_order_id','=','order_id')
							->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
							->where('order_status','=','Delivered')
							->where('customer_name','LIKE','%'.Input::get('keyword').'%')
							->groupBy('order_id')
							->orderBy('order_date','DESC')
							->paginate(5);
				return View::make('admin.order.delivered.deliveredorder')->with('deliveredorder',$deliveredorder);
			}
		}else{
        	if(Session::get('filter')=="date"){
        			if(Session::get('dateby')=='all'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateorder'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Pending')
								->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateapprove'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Approve')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateprocess'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','On Process')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='datefinished'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Finished')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='datedelivered'){
					$deliveredorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Delivered')
								->where('order_status_type','=','Delivered')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}
        	}else if(Session::get('filter')=="type"){
					if(Session::get('list')=='all'){
					$deliveredorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Delivered')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else{
					$deliveredorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Delivered')
									->where('order_name','=',Session::get('list'))
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
        	}else if(Session::get('filter')=="keyword"){
        			if(Session::get('filterby')=='code'){
						$deliveredorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Delivered')
									->where('order_code','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else if(Session::get('filterby')=='name'){
						$deliveredorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Delivered')
									->where('customer_name','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
        	}
		}
		return View::make('admin.order.delivered.deliveredorder')->with('deliveredorder',$deliveredorder);
		



	}

	public function getDetaildeliveredorder($id)
	{	
		$deliveredorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->join('ref_idcard_type','order_idcard_idcard_type_id','=','idcard_type_id')
						->join('ref_idcard_quality','order_idcard_idcard_quality_id','=','idcard_quality_id')
						->where('order_status','=','Delivered')
						->where('order_id','=',$id)
						->first();
		$detailorder =  DB::table('sys_order')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->join('sys_order_idcard_detail','order_idcard_detail_order_idcard_id','=','order_idcard_id')
						->join('ref_additional_features','order_idcard_detail_additional_features_id','=','additional_features_id')
						->where('order_status','=','Delivered')
						->where('order_id','=',$id)
						->get();
		return View::make('admin.order.delivered.detaildeliveredorder', compact('deliveredorder',$deliveredorder,'detailorder',$detailorder));
	}


}

